/**
 * 创建 mysql 连接，并暴露一个 sql 语句执行函数 execSql
 */
const mysql = require('mysql')
const { MYSQL_CONF } = require('../config/db')

const pool = mysql.createPool(MYSQL_CONF)

const execSql = (sql, param) => {
  const promise = new Promise((resolve, reject) => {
    pool.getConnection((err, connection) => {
      if (err) throw err
      connection.query(sql, param, (err, result) => {
        connection.release()
        if (err) {
          reject(err)
          return
        }
        resolve(result)
      })
    })
  })
  return promise
}

module.exports = {
  execSql
}
